package com.zhb.zsharedpre

import android.content.Context
import android.content.SharedPreferences


/**
 * SharedPreferences存储
 * Created by HongboZhao on 2021/1/25.
 */
class ZSharedPre {

    private var sharePre: SharedPreferences? = null

    /**
     * 初始化
     */
    fun init(context: Context) {
        init(context, ZSharedPre::class.java.simpleName)
    }

    fun init(context: Context, fileName: String?) {
        sharePre = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
    }

    private fun checkInit() {
        if (sharePre == null) {
            throw NullPointerException("Please call the initialization method init in Application first")
        }
    }

    fun saveInt(key: String?, value: Int) {
        checkInit()
        val editor = sharePre!!.edit()
        editor.putInt(key, value)
        editor.commit()
    }

    /**
     * Set String value into SharePreference
     *
     * @param key
     * @param value
     */
    fun saveString(key: String?, value: String?) {
        checkInit()
        val editor = sharePre!!.edit()
        editor.putString(key, value)
        editor.commit()
    }

    /**
     * Set String value into SharePreference
     *
     * @param key
     * @param value
     */
    fun saveLong(key: String?, value: Long) {
        checkInit()
        val editor = sharePre!!.edit()
        editor.putLong(key, value)
        editor.commit()
    }

    /**
     * Set String value into SharePreference
     *
     * @param key
     * @param value
     */
    fun saveFloat(key: String?, value: Float) {
        checkInit()
        val editor = sharePre!!.edit()
        editor.putFloat(key, value)
        editor.commit()
    }

    /**
     * Set Boolean value into SharePreference
     *
     * @param key
     * @param value
     */
    fun saveBoolean(key: String?, value: Boolean) {
        checkInit()
        val editor = sharePre!!.edit()
        editor.putBoolean(key, value)
        editor.commit()
    }

    /**
     * Remove key from SharePreference
     *
     * @param key
     */
    fun removeKey(key: String?) {
        checkInit()
        val editor = sharePre!!.edit()
        editor.remove(key)
        editor.commit()
    }

    /**
     * 查询是否包含key的值，存在为true，否则为false
     *
     * @param key
     * @return
     */
    operator fun contains(key: String?): Boolean {
        checkInit()
        return sharePre!!.contains(key)
    }

    /**
     * 清空存储的内容
     */
    fun clear() {
        checkInit()
        val editor = sharePre!!.edit()
        editor.clear()
        editor.commit()
    }

    /**
     * 获取所有内容
     *
     * @return
     */
    fun getAll(): Map<String?, *>? {
        checkInit()
        return sharePre!!.all
    }

    /**
     * Get Integer Value
     *
     * @param key
     * @return
     */
    fun getInt(key: String?): Int {
        checkInit()
        return sharePre!!.getInt(key, -1)
    }

    /**
     * Get Integer Value
     *
     * @param key
     * @param defValue
     * @return
     */
    fun getInt(key: String?, defValue: Int): Int {
        checkInit()
        return sharePre!!.getInt(key, defValue)
    }

    /**
     * Get Float Value
     *
     * @param key
     * @param defValue
     * @return
     */
    fun getFloat(key: String?, defValue: Float): Float {
        checkInit()
        return sharePre!!.getFloat(key, defValue)
    }

    /**
     * Get String Value
     * 通过输入的key来获得value
     *
     * @param key
     * @return
     */
    fun getString(key: String?): String? {
        checkInit()
        return sharePre!!.getString(key, null)
    }

    /**
     * Get String Value
     * 通过输入的key来获得value, 若value不存在，返回自定义的默认值defaultstr
     *
     * @param key
     * @param defaultstr
     * @return
     */
    fun getString(key: String?, defaultstr: String?): String? {
        checkInit()
        return sharePre!!.getString(key, defaultstr)
    }

    /**
     * Get Long Value
     *
     * @param key
     * @return
     */
    fun getLong(key: String?): Long {
        checkInit()
        return sharePre!!.getLong(key, 0)
    }

    /**
     * Get Long Value
     *
     * @param key
     * @param defaultLong
     * @return
     */
    fun getLong(key: String?, defaultLong: Long): Long {
        checkInit()
        return sharePre!!.getLong(key, defaultLong)
    }

    /**
     * Get Boolean Value
     *
     * @param key
     * @return
     */
    fun getBoolean(key: String?): Boolean {
        checkInit()
        return sharePre!!.getBoolean(key, false)
    }

    /**
     * Get Boolean Value
     *
     * @param key
     * @param defaultBoolean
     * @return
     */
    fun getBoolean(key: String?, defaultBoolean: Boolean): Boolean {
        checkInit()
        return sharePre!!.getBoolean(key, defaultBoolean)
    }

    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            ZSharedPre()
        }
    }
}